<template>
  <!-- 选择运营商弹出框 -->
  <el-dialog title="运营商信息" v-model="editVisible" width="70%" top="2vh" draggable @closed="closedFormDialog">
    <div class="container">
      <div class="mb10">
        <el-text class="mx-1"> 查询条件：</el-text>
      </div>
      <el-row :gutter="10">
        <el-col :xl="6" :lg="6" :md="12" :sm="24">
          <el-input v-model.trim="searchCondition.company_name" placeholder="营业执照名称" clearable></el-input>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24">
          <el-input v-model.trim="searchCondition.username" placeholder="联系人名称" clearable></el-input>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24">
          <el-input v-model.trim="searchCondition.mobile" placeholder="手机号" clearable></el-input>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24">
          <el-space wrap>
            <el-button type="success" :icon="Search" :disabled="disabledbtn" @click="searchInfo">搜索</el-button>
            <el-button type="warning" :icon="Refresh" :disabled="disabledbtn" @click="resetSearch">重置</el-button>
          </el-space>
        </el-col>
      </el-row>
      <div class="mb10">
        <el-text class="mx-1"> 查询结果：</el-text>
      </div>
      <el-table :data="tableList" border class="table" row-key="id" ref="multipleTableRef" v-loading="loading" :reserve-selection="true" @row-dblclick="rowDblclick">
        <el-table-column type="index" align="center"> </el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="company_name" label="营业执照名称" align="center"> </el-table-column>
        <el-table-column prop="username" label="联系人" align="center"> </el-table-column>
        <el-table-column prop="mobile" label="手机号" align="center"> </el-table-column>
        <el-table-column prop="bank_account" label="银行卡号" align="center"> </el-table-column>
        <el-table-column prop="bank_name" label="开户行" align="center"> </el-table-column>
        <el-table-column prop="usage_status_name" label="审核状态" align="center"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间" align="center" :formatter="(row: IOperatorManagerList) => handleVerifyFormatDate(row.add_time)"> </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @update:current-page="handleCurrentChange"
          @update:page-size="handleSizeChange"
          layout="total, sizes, prev, pager, next, jumper"
          :page-size="searchCondition.size"
          :current-page="searchCondition.current"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :total="total"
          :hide-on-single-page="false">
        </el-pagination>
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancelGoodsDialog">取 消</el-button>
        <el-button type="primary" :disabled="disabledbtn" @click="handleSelectedGoods">确 定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { Search, Refresh } from "@element-plus/icons-vue";
import { handleVerifyFormatDate } from "@/utils/utils";
import { useState } from "@/hooks/useState.ts";
import type { IOperatorManagerList, ISearchOperatorManager } from "@/types/user/operatorManager";
import { getOperatorList } from "@/utils/api/admin/merchant/operator";
const { disabledbtn, loading, toggleState } = useState();
interface Props {
  selectNumber?: "single" | "more";
}
const { selectNumber = "single" } = defineProps<Props>();
const emit = defineEmits<{
  handleSuccess: [arr: IOperatorManagerList[]];
}>();

const editVisible = defineModel<boolean>("editVisible", { required: true });
const multipleTableRef = useTemplateRef("multipleTableRef"); /* 列表组件的引用 */
const total = ref(0);
const searchCondition = reactive<ISearchOperatorManager>({
  company_name: "",
  usage_status: "ENABLE",
  username: "",
  mobile: "",
  current: 1,
  size: 10
});
const tableList = ref<Array<IOperatorManagerList>>([]);

onMounted(() => {
  getTableInfo();
});

watch(
  () => editVisible.value,
  newValue => {
    if (newValue) {
      /* 初始化数据 */
      resetSearch();
    }
  }
);

// 关闭模态框
function closedFormDialog() {
  handleCancelGoodsDialog();
}

/* 显示第几页 */
function handleCurrentChange(val: number) {
  searchCondition.current = val;
  getTableInfo();
}

/* 每页显示几条 */
function handleSizeChange(val: number) {
  searchCondition.current = 1;
  searchCondition.size = val;
  getTableInfo();
}

/* 搜索 */
function searchInfo() {
  searchCondition.current = 1;
  searchCondition.size = 10;
  getTableInfo();
}

/**
 * 重置搜索内容
 */
function resetSearch() {
  searchCondition.company_name = "";
  searchCondition.username = "";
  searchCondition.mobile = "";
  searchInfo();
}

/* 双击当前行 */
/* 双击当前行 */
function rowDblclick(row: IOperatorManagerList) {
  multipleTableRef.value!.toggleRowSelection(row);
}

/* 请求：获取表格中数据 */
function getTableInfo() {
  toggleState(true);
  getOperatorList<IOperatorManagerList, ISearchOperatorManager>(searchCondition)
    .then(res => {
      tableList.value = res.result.data;
      total.value = res.result.total;
    })
    .finally(() => {
      toggleState(false);
    });
}

/* 关闭模态框 */
function handleCancelGoodsDialog() {
  editVisible.value = false;
}
/* 选中商品数据 */
function handleSelectedGoods() {
  const selection = multipleTableRef.value!.store.states.selection.value;
  if (selection.length === 0) {
    ElMessage.warning("请至少选择一条数据");
    return;
  }
  if (selection.length > 1 && selectNumber == "single") {
    ElMessage.warning("至多选择一条数据");
    return;
  }
  emit("handleSuccess", selection);
  handleCancelGoodsDialog();
  multipleTableRef.value!.clearSelection();
}
</script>

<style lang="scss" scoped></style>
